<template>
	<view class="container">
		<view class="user">
			<image class="user-left" src="/static/img/user-img.png" />
			<view class="user-name">{{userInfo.RealName}}</view>
			<view class="user-greet">你好</view>
			<image class="user-right" src="/static/img/user-img.png" />
		</view>
		<mymenu></mymenu>
		<mynewnotice :noticeList="noticeList"></mynewnotice>
		<mytabbar :currentId="currentId"></mytabbar>
		<myhome :show-my-home="showMyHome" :family-member-num="familyMemberNum"
		:children-member-num="childrenMemberNum" :invite-img-url="inviteImgUrl"></myhome>
	</view>
</template>

<script>
	import mytabbar from "../../components/mytabbar.vue"
	import mymenu from "../../components/menu.vue"
	import mynewnotice from "../../components/newnotice.vue"
	import myhome from "../../components/myhome.vue"
	import {
		myRequest
	} from '../../utils'

	export default {
		components: {
			mytabbar,
			mymenu,
			mynewnotice,
			myhome
		},

		data() {
			return {
				showMyHome: false,
				currentId: '1',
				noticeList: [],
				userInfo: null,
				timer: null,
				familyMemberNum: 0,
				childrenMemberNum: 0,
				inviteImgUrl: ''
			}
		},

		onShow() {

			let that = this
			that.showMyHome = false

			myRequest({
				url: '/api/user/check_login',
				data: {},
			}, function(res) {
				
				if (res.statusCode != 200) {
					uni.showModal({
						title: '提示',
						content: '服务器错误，请求失败'
					})
					return
				}

				if (res.data.status != 0) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}

				that.familyMemberNum = res.data.data.FamilyMemberNum
				that.childrenMemberNum = res.data.data.ChildrenMemberNum
				that.userInfo = res.data.data
				
			})
			
			myRequest({
				url: '/api/user/invite_qrcode',
				data: {},
			}, function(res) {
				
				if (res.statusCode != 200) {
					uni.showModal({
						title: '提示',
						content: '服务器错误，请求失败'
					})
					return
				}
			
				that.inviteImgUrl = res.data.data
				
			})

			if (that.timer != null) {
				clearTimeout(that.timer)
			}

			uni.showLoading({
				title: "拼命加载中...",
				success() {

					that.timer = setTimeout(function() {

						myRequest({
							url: '/api/new_notice',
							data: {},
						}, function(res) {

							uni.hideLoading()

							if (res.statusCode != 200) {
								uni.showModal({
									title: '提示',
									content: '服务器错误，请求失败'
								})
								return
							}

							if (res.data.status != 0) {
								uni.navigateTo({
									url: '/pages/login/login'
								})
								return
							}

							that.noticeList = res.data.data

						})

					}, 1000)

				}
			})

		},

		methods: {
			
			showHome() {
				this.showMyHome = true
			},

			closeHome() {
				this.showMyHome = false
			},
			
		}
	}
</script>

<style>
	.container {
		width: 750rpx;
		height: auto;
		background-image: linear-gradient(#ffae00, #eee, #efefef);
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 160rpx;
		min-height: 1800rpx;
	}

	.user {
		width: 650rpx;
		display: flex;
		margin-top: 30rpx;
	}

	.user-left {
		width: 30rpx;
		height: 20rpx;
	}

	.user-name {
		font-size: 60rpx;
	}

	.user-greet {
		font-size: 40rpx;
		margin-top: 24rpx;
		margin-left: 14rpx;
	}

	.user-right {
		width: 30rpx;
		height: 20rpx;
		margin-top: 72rpx;
	}
</style>
